package bid.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@WebFilter(filterName = "role", value = "/*")
public class RoleFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String path = request.getServletPath();
        //System.out.println(path+">>>>>>>>>");
        if (path.equals("/login.user") || path.endsWith(".jsp") || path.endsWith(".css")
                || path.endsWith(".js") || path.endsWith(".jpg")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        Object obj = request.getSession().getAttribute("LOGIN_USER");

        if (obj == null) {
            request.setAttribute("msg", "您还没有登录");
            request.getRequestDispatcher("/login.jsp").forward(request, servletResponse);
            return;
        }
        long start = System.nanoTime();
        //放行！！！
        filterChain.doFilter(servletRequest, servletResponse);
        long end = System.nanoTime();

        System.out.println("请求地址:" + path + "耗时" + (end - start) + "纳秒");
    }

    @Override
    public void destroy() {

    }
}
